<?php
class CategoryController extends AdminAppController{
    
    function index($parent_id = null)
    {
        $this->loadModel('Admin.Category');
        $category = $this->Category->get_all_category('all', $parent_id);
        if($parent_id == null)
            $list_category = $this->Category->filter_category($category);
        else 
            $list_category = $category;
        $this->set('list_category', $list_category);
        
        $parent_category = $this->Category->get_parent_category();

        $this->set('parent_category', $parent_category);
        
        $this->set('parent_id', $parent_id);
        /*
        $this->Session->setFlash(
                                'FAIL',
                                'default',
                                array('class' => 'alert alert-danger')
                            );                
        $this->Session->setFlash(
                                'SUCCESS',
                                'default',
                                array('class' => 'alert alert-success')
                            );
        */
    }
    
    function add($parent_id = null)
    {
        $this->loadModel('Admin.Language');
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        $this->set('parent_id', $parent_id);
        if($this->request->isPost())
        {
            $this->loadModel('Admin.Category');
            $this->loadModel('Admin.CategoryDesc');
            $this->loadModel('Admin.Image');
            $data = array(
                        'parent_id' => $_POST['parent_id'],
                        'category_code' => $this->request->data['Category']['category_code'],
                        'date_added' => date("Y-m-d H:i:s"),
                        'date_modified' => date("Y-m-d H:i:s"),
                        'status' => $_POST['status'],
                        'other' => $this->request->data['Category']['other'],
                        'sort_order' => $this->request->data['Category']['sort_order'],
                        );
            $category_id = $this->Category->add($data);

            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'category_id' => $category_id,
                        'title' => $this->request->data["Category"]["title_$lang_code"],
                        'description' => $_POST["description_$lang_code"],
                        'long_description' => $_POST["long_description_$lang_code"],
                        'meta_title' => $_POST["meta_title_$lang_code"],
                        'meta_description' => $_POST["meta_description_$lang_code"],
                        'meta_keywords' => $_POST["meta_keywords_$lang_code"],
                        'lang_code' => $lang_code,
                        );
                $this->CategoryDesc->add($data);
            }
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->add($category_id, 'category', $data);
            
            
            $this->loadModel('Admin.Link');
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $this->request->data["Category"]["link_$lang_code"],
                        'controller' => 'category',
                        'method' => 'index',
                        'post_id' => $category_id,
                        'type' => 'category',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            
            $this->Session->setFlash(
                                'Thêm mới thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "category", 
                                  "action" => "index", $parent_id));
        }     
    }

    function edit($category_id = "", $parent_id = null)
    {
        if($category_id == ""){
            $this->redirect(array("controller" => "category", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('parent_id', $parent_id);
        $this->set('category_id', $category_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        $this->loadModel('Admin.CategoryDesc');
        $this->loadModel('Admin.Image');

        $category_info = $this->Category->get_category_by_id($category_id, 'all');
        $this->set('category_info', $category_info);
        //print_r($category_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        $category = $this->Category->get_category();
        
        
        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        $list_image = $this->Image->get($category_id, 'category');
        $this->set('list_image', $list_image);

        if($this->request->isPost())
        {
            $data = array(
                        'parent_id' => $_POST['parent_id'],
                        'category_code' => "'".$this->request->data['Category']['category_code']."'",
                        'date_modified' => "'".date("Y-m-d H:i:s")."'",
                        'status' => $_POST['status'],
                        'other' => "'".$this->request->data['Category']['other']."'",
                        'sort_order' => "'".$this->request->data['Category']['sort_order']."'",
                        );
            $this->Category->edit($category_id, $data);
            
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data["Category"]["title_$lang_code"]."'",
                        'description' => "'".$_POST["description_$lang_code"]."'",
                        'long_description' => "'".$_POST["long_description_$lang_code"]."'",
                        'meta_title' => "'".$_POST["meta_title_$lang_code"]."'",
                        'meta_description' => "'".$_POST["meta_description_$lang_code"]."'",
                        'meta_keywords' => "'".$_POST["meta_keywords_$lang_code"]."'",
                        );
                $this->CategoryDesc->edit($category_id, $lang_code, $data);
            }
            
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->edit($category_id, 'category', $data);
            
            
            $this->loadModel('Admin.Link');
            $this->Link->deleteAll(array('post_id' => $category_id, 'type' => 'category'));
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $this->request->data["Category"]["link_$lang_code"],
                        'controller' => 'category',
                        'method' => 'index',
                        'post_id' => $category_id,
                        'type' => 'category',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            $this->Session->setFlash(
                                'Cập nhật thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array("controller" => "category", 
                                  "action" => "index", $parent_id));
            
        }
    }
}